package com.zaimokuza.cn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.blog.BlogTagRelation;
import com.zaimokuza.cn.entity.blog.Tag;
import com.zaimokuza.cn.entity.enums.CustomizeExceptionEnum;
import com.zaimokuza.cn.mapper.TagMapper;
import com.zaimokuza.cn.service.TagService;
import com.zaimokuza.cn.util.AssertUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author zaimokuza
 */
@Service
public class TagServiceImpl implements TagService {

    @Resource
    private TagMapper tagMapper;

    @Override
    public void addTag(Tag tag) {
        // 校验名称不能重复
        Integer nameIsRepeat = tagMapper.queryNameIsRepeat(tag.getId(), tag.getUserId(), tag.getName());
        AssertUtils.isNull(nameIsRepeat, CustomizeExceptionEnum.TAG_NAME_IS_REPEAT);
        // 添加记录
        tagMapper.insertRecord(tag);
    }

    @Override
    public void addBlogTagRelation(String blogId, ArrayList<String> tagIds) {
        // 标签集合无有效信息时直接返回
        if (CollectionUtils.isEmpty(tagIds)) {
            return;
        }
        // 整理博客标签关联关系集合
        ArrayList<BlogTagRelation> relations = new ArrayList<>();
        for (String tagId : tagIds) {
            relations.add(new BlogTagRelation(tagId, blogId));
        }
        // 批量添加
        tagMapper.insertBatchRelation(relations);
    }

    @Override
    public void updateTag(Tag tag) {
        // 校验名称不能重复
        Integer nameIsRepeat = tagMapper.queryNameIsRepeat(tag.getId(), tag.getUserId(), tag.getName());
        AssertUtils.isNull(nameIsRepeat, CustomizeExceptionEnum.TAG_NAME_IS_REPEAT);
        // 更新记录
        tagMapper.updateRecord(tag);
    }

    @Override
    public void updateBlogTagRelation(String blogId, ArrayList<String> tagIds) {
        // 批量删除原始博客标签关联关系
        tagMapper.deleteBatchRelationByBlogId(blogId);
        // 批量添加新的博客标签关联关系
        addBlogTagRelation(blogId, tagIds);
    }

    @Override
    public void deleteTag(String id) {
        // 删除标签博客关联信息
        tagMapper.deleteBatchRelation(id);
        // 删除标签信息
        tagMapper.deleteRecord(id);
    }

    @Override
    public void deleteBatchRelationByBlogId(String blogId) {
        tagMapper.deleteBatchRelationByBlogId(blogId);
    }

    @Override
    public ArrayList<Tag> getTagList(String userId) {
        return tagMapper.queryTagList(userId);
    }

    @Override
    public ArrayList<Tag> getTagListByBlogId(String blogId) {
        return tagMapper.queryTagListByBlogId(blogId);
    }

    @Override
    public ArrayList<String> getTagIdListByBlogId(String blogId) {
        return tagMapper.queryTagIdListByBlogId(blogId);
    }

    @Override
    public PageResult<Tag> getTagPage(String name, String userName, Integer page, Integer size) {
        // 分页
        PageHelper.startPage(page, size);
        // 查询
        Page<Tag> tagPage = tagMapper.queryTagPage(name, userName);
        // 封装结果并返回
        return PageResult.from(tagPage);
    }

}
